package org.dromara.system.service.impl.trading.channel;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessException;
import org.dromara.system.config.core.RedisDelayMQSender;
import org.dromara.system.converter.MachineConverters;
import org.dromara.system.domain.ProductLineInformation;
import org.dromara.system.domain.TradingRecords;
import org.dromara.system.domain.bo.yhb.GTJHTradingWaterBo;
import org.dromara.system.domain.bo.yhb.GTJHTradingWaterSignBo;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.enums.RedisMQConstant;
import org.dromara.system.service.impl.trading.AbstractTradingService;
import org.dromara.system.util.CommonConstant;
import org.dromara.system.util.JsonUtil;
import org.dromara.system.util.zzb.RsaUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service("gTJHTradingService")
@RequiredArgsConstructor
public class GTJHTradingService extends AbstractTradingService<Object, GTJHTradingWaterBo> {

    @Resource
    private RedisDelayMQSender redisDelayMQSender;

    @Override
    public NotificationEnum getNotificationEnum() {
        return NotificationEnum.GT_JH;
    }

    @Override
    public void tradingWaterConvert(GTJHTradingWaterBo bo) {
        TradingRecords tradingRecords = MachineConverters.INSTANCE.gTJHRecordsToReceiveDealRecords(bo);
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(CommonConstant.DATE_TIME_FORMAT_PATTERN);
            Date date = formatter.parse(bo.getOrderStartTime());
            tradingRecords.setTradingTime(date);
        } catch (Exception e) {
            tradingRecords.setTradingTime(DateUtil.date());
        }
        BigDecimal rate = Convert.toBigDecimal(bo.getTradeFeeRate());
        tradingRecords.setDealRate(Convert.toStr(rate));
        R<TradingRecords> tradingRecordsR = dealUserTrading(tradingRecords);
    }

    @Override
    public Object tradingWaterAssemble(String mapBody, Integer mark) {
        log.info("国通聚合交易入参=={}", mapBody);
        ProductLineInformation productLineInformation = getBrandProductsLineById(mark);
        BusinessException.isNull(productLineInformation, "关联权限不存在");
        String decrypt = getParams(mapBody, productLineInformation);
        GTJHTradingWaterBo zzbTradingWaterBo = JSONUtil.toBean(decrypt, GTJHTradingWaterBo.class);
        redisDelayMQSender.send(RedisMQConstant.TRADING_QUEUE,
            JsonUtil.toJSONString(zzbTradingWaterBo), getNotificationEnum(), Convert.toStr(zzbTradingWaterBo.getOrderNo()));
        return "success";
    }


    private String getParams(String mapBody, ProductLineInformation productLineInformation) {
        GTJHTradingWaterSignBo zzbTradingWaterSignBo = JSONUtil.toBean(mapBody, GTJHTradingWaterSignBo.class);
        String decrypt = null;
        try {
            decrypt = RsaUtil.decrypt(zzbTradingWaterSignBo.getData(),
                RsaUtil.getPrivateKey(productLineInformation.getAgentPriKey()));
        } catch (Exception e) {
            BusinessException.isTrue(Boolean.TRUE, "解密失败！");
        }
        return decrypt;
    }

}
